package com.cmdandroid.icebutton;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

//This is the screen where the user adds contacts to ICE Contacts list
public class ICEButtonAddContactsActivity extends ICEButtonActivity{
	 static final int PICK_CONTACT=0;
	
	 Object ContactsList;
	 int[] contacts;
//	 String name;
//	 String number;
//	 String[] contact_data=null;

	 
	 Cursor emails;
	@Override
	
    public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		// ContentResolver cr = getContentResolver();
		// final ContactAccessor mContactAccessor  = ContactAccessor.getInstance();
		 Intent intent = new Intent(Intent.ACTION_PICK, ContactsAdapterBridge.URI);
		 startActivityForResult(intent,PICK_CONTACT);
		 //startActivityForResult(mContactAccessor.getContentPickerIntent(), PICK_CONTACT); //Go to Contact List picker
	}
	
	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data){
		super.onActivityResult(reqCode, resultCode, data);
		//Check which activity has been returned from
		switch (reqCode) {
		//Pick contact activity
		case (PICK_CONTACT):
			//Did the activity end OK?
			if (resultCode == Activity.RESULT_OK) {
				
				List<String> infoArrayList= new ArrayList<String>();
				infoArrayList = ContactsAdapterBridge.INSTANCE.getInfo(this, data);
				
				String name = infoArrayList.get(0);
				List<String> numbers = new ArrayList<String>();
				
				//Go through rest of info and grab all phone numbers
				// Note i initialized to 1
				for (Integer i=1; i<infoArrayList.size(); i++) {
					numbers.add(infoArrayList.get(i));
				}
				
				Intent result = new Intent();
				//Bundle up results to return to previous activity
				Bundle b = new Bundle();
				//Go through numbers and bundle up all phone numbers
				for (Integer i=0; i<numbers.size(); i++) {
					b.putString(CONTACTDATAKEY_NUMBER + i, numbers.get(i));
				}
				b.putString(CONTACTDATAKEY_NAME,name );
				//b.putString(CONTACTDATAKEY_NUMBER,infoArrayList.get(1));
				result.putExtras(b);
				//Put data into Results
				setResult(Activity.RESULT_OK, result);

			}
		}
		//finish activity and return to previous
		finish();
	}

}


		 
	/*	Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsList.Contacts.CONTENT_URI); 
		 startActivityForResult(intentContact, PICK_CONTACT)
		 ;}	//onCreate
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent)
	{ 
		if (requestCode == PICK_CONTACT) 
		{             
			getContactInfo(intent);            
			}
		}
	protected void getContactInfo(Intent intent)
	{
		Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);       
		while (cursor.moveToNext()) 
		{
			String contactId = cursor.getString(cursor.getColumnIndex(ContactsList.Contacts._ID));
			name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsList.Contacts.DISPLAY_NAME));
			String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsList.Contacts.HAS_PHONE_NUMBER));
			
		       if ( hasPhone.equalsIgnoreCase("1"))
		    	   hasPhone = "true";
		       else         
		    	   hasPhone = "false";
		       
		       if (Boolean.parseBoolean(hasPhone)) 
		       {    
		    	   Cursor phones = getContentResolver().query(ContactsList.CommonDataKinds.Phone.CONTENT_URI, null,ContactsList.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);        
		    	  
				while (phones.moveToNext()) 
		    	   {       
		    		emailAddress = emails.getString(emails.getColumnIndex(ContactsList.CommonDataKinds.Email.DATA));  
		    	   }
		    	   emails.close();
		    	   
		    	   Cursor address = getContentResolver().query( 
		    			   ContactsList.CommonDataKinds.StructuredPostal.CONTENT_URI, 
		    			   null,
		    			   ContactsList.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,
		    			   null, null);
		    	   
		    	   {       // These are all private class variables, don't forget to create them.     
		    		   Object street = address.getString(address.getColumnIndex(ContactsList.CommonDataKinds.StructuredPostal.STREET)); 
		    		   Object city = address.getString(address.getColumnIndex(ContactsList.CommonDataKinds.StructuredPostal.CITY));
		    		   Object state = address.getString(address.getColumnIndex(ContactsList.CommonDataKinds.StructuredPostal.REGION)); 
		    		   Object postalCode = address.getString(address.getColumnIndex(ContactsList.CommonDataKinds.StructuredPostal.POSTCODE));     
		    		   Object country = address.getString(address.getColumnIndex(ContactsList.CommonDataKinds.StructuredPostal.COUNTRY));   
		    		   Object type       = address.getString(address.getColumnIndex(ContactsList.CommonDataKinds.StructuredPostal.TYPE));    
		    		   }//address.moveToNext()    
		    	   }  //while (cursor.moveToNext())        
		       			//cursor.close();}//getContactInfo
		    	 }
		       
			 
		

	
		startActivityForResult(intentContact, PICK_CONTACT);
        
        
        setContentView(R.layout.main); //TODO: change to the addContacts layout
	*/

